<template>
  <div class="login-container min-h-screen flex justify-center items-center bg-light-bg-primary dark:bg-dark-bg-primary blue:bg-blue-bg-primary transition-colors duration-300">
    <div class="login-form-wrapper w-[400px] sm:w-[90%] max-w-[400px] p-8 bg-light-bg-secondary dark:bg-dark-bg-secondary blue:bg-blue-bg-secondary rounded-xl shadow-lg border border-light-border dark:border-dark-border blue:border-blue-border transition-colors duration-300">
      <div class="login-header text-center mb-8">
        <h2 class="text-2xl font-bold text-accent-color mb-2">新易访内网系统</h2>
        <p class="text-light-text-secondary dark:text-dark-text-secondary blue:text-blue-text-secondary">请登录您的账号</p>
      </div>
      
      <el-form 
        :model="loginForm"
        :rules="loginRules"
        ref="loginFormRef"
        label-width="80px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            class="bg-light-bg-primary dark:bg-dark-bg-primary blue:bg-blue-bg-primary border-light-border dark:border-dark-border blue:border-blue-border"
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
            class="bg-light-bg-primary dark:bg-dark-bg-primary blue:bg-blue-bg-primary border-light-border dark:border-dark-border blue:border-blue-border"
          />
        </el-form-item>
        
        <el-form-item>
          <div class="login-options flex justify-between items-center">
            <el-checkbox v-model="loginForm.rememberMe" class="text-light-text-primary dark:text-dark-text-primary blue:text-blue-text-primary">记住密码</el-checkbox>
            <router-link to="/forgot-password" class="forgot-password-link text-accent-color hover:text-accent-color/80 transition-colors">忘记密码？</router-link>
          </div>
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="handleLogin"
            class="login-btn w-full h-10 text-base bg-accent-color hover:bg-accent-color/90 text-white"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
        
        <el-form-item>
          <div class="register-link text-center mt-4">
            <span class="text-light-text-secondary dark:text-dark-text-secondary blue:text-blue-text-secondary">还没有账号？</span>
            <router-link to="/register" class="text-accent-color hover:text-accent-color/80 ml-1 transition-colors">立即注册</router-link>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { useAuthStore } from '../../stores/auth'
import { useThemeStore } from '../../stores/theme'
import { ElMessage } from 'element-plus'
import router from '../../router'

// 使用auth store
const authStore = useAuthStore()
const themeStore = useThemeStore()

// 表单引用
const loginFormRef = ref()

// 加载状态
const loading = ref(false)

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: '',
  rememberMe: false
})

// 登录表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应在3到20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应在6到20个字符之间', trigger: 'blur' }
  ]
}

// 处理登录
const handleLogin = async () => {
  try {
    // 表单验证
    await loginFormRef.value?.validate()
    
    // 设置加载状态
    loading.value = true
    
    // 调用登录方法
    const success = await authStore.login(loginForm.username, loginForm.password)
    
    if (success) {
      ElMessage.success('登录成功')
      
      // 根据用户角色跳转到不同页面
      const userRole = authStore.getUserRole
      
      if (userRole === 'super-admin') {
        router.push('/super-admin')
      } else if (userRole === 'admin') {
        router.push('/admin')
      } else {
        router.push('/')
      }
    } else {
      ElMessage.error(authStore.error || '登录失败')
    }
  } catch (error) {
    console.error('Login validation error:', error)
  } finally {
    loading.value = false
  }
}

// 组件挂载时初始化主题
onMounted(() => {
  themeStore.initTheme()
})
</script>

<style scoped>
/* 确保组件内所有元素都有过渡效果 */
* {
  transition-property: color, background-color, border-color, text-decoration-color, fill, stroke;
  transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
  transition-duration: 300ms;
}

html, body, #app {
  height: 100%;
  margin: 0;
  padding: 0;
}

/* 响应式调整已通过Tailwind的响应式类实现 */
</style>